const app = getApp();
var utils = require('../../untils/util.js');
Page({
  data: {
    raceList: [],//族
    objRaceList: [],
    zodiacList: [],//生肖
    objZodiacList: [],
    constellationList: [],//星座
    objConstellationListC: [],
    bloodTypeList: [],//血型
    objBloodTypeList: [],
    childList: [],//有无子女
    objChildList: [],
    carList: [],
    objCarList: [],
    houseList: [],
    objHouseList: [],
    reqData: {
      'xgmember.nation': '',
      'xgmember.zodiac': '',
      'xgmember.constellation': '',
      'xgmember.blood': '',
      'xgmember.children': '',
      'xgmember.car': '',
      'xgmember.room': ''
    },
    nation: '',
    zodiac: '',
    constellation: '',
    blood: '',
    children: '',
    car: '',
    room: ''
  },
  onLoad: function () {
    this.initPage();
  },
  initPage: function () {
    var _this = this;
    app.doReq(app.globalData.DICTIONARY, {
      parentId: '424,437,450,7,463,467,457',//生肖
    }, 'get', function (res) {
      console.log(res);
      let data = res.data;
      if (data.code == 200) {
        let zodiacList = data.obj[424];
        let zodiacListTemp = [];
        for (let i of zodiacList) {
          zodiacListTemp.push(i.name);
        }

        let constellationList = data.obj[437];
        let constellationListTemp = []
        for (let i of constellationList) {
          constellationListTemp.push(i.name);
        }

        let bloodTypeList = data.obj[450];
        let bloodTypeListTemp = [];
        for (let i of bloodTypeList) {
          bloodTypeListTemp.push(i.name);
        }

        let raceList = data.obj[7];
        let raceListTemp = [];
        for (let i of raceList) {
          raceListTemp.push(i.name);
        }

        let carList = data.obj[463];
        let carListTemp = [];
        for (let i of carList) {
          carListTemp.push(i.name);
        }

        let houseList = data.obj[467];
        let houseListTemp = [];
        for (let i of houseList) {
          houseListTemp.push(i.name);
        }

        let childList = data.obj[457];
        let childListTemp = [];
        for (let i of childList) {
          childListTemp.push(i.name);
        }

        _this.setData({
          raceList: raceListTemp,
          objRaceList: raceList,
          zodiacList: zodiacListTemp,
          objZodiacList: zodiacList,
          constellationList: constellationListTemp,
          objConstellationListC: constellationList,
          bloodTypeList: bloodTypeListTemp,
          objBloodTypeList: bloodTypeList,
          childList: childListTemp,
          objChildList: childList,
          carList: carListTemp,
          objCarList: carList,
          houseList: houseListTemp,
          objHouseList: houseList,
        });

        app.getUserInfoSyn({"openId":wx.getStorageSync('openid')}).then(function () {
          var userInfo = wx.getStorageSync('userInfo');
          var member = userInfo.member;
          _this.setData({
            nation: member.nations,
            zodiac: member.zodiacs,
            constellation: member.constellations,
            blood: member.bloods,
            children: member.childrens,
            car: member.cars,
            room: member.rooms,
            reqData: {
              'xgmember.nation': member.nation,
              'xgmember.zodiac': member.zodiac,
              'xgmember.constellation': member.constellation,
              'xgmember.blood': member.blood,
              'xgmember.children': member.children,
              'xgmember.car': member.car,
              'xgmember.room': member.room
            },
          })
        });
      }
    });
  },
  whenChange: function (e) {
    var _this = this;
    let index = e.detail.value;
    var t = e.currentTarget.dataset.type;
    if (t == 'race') {
      _this.data.reqData['xgmember.nation'] = _this.data.objRaceList[index].id;
      _this.setData({
        nation: _this.data.objRaceList[index].name
      });
    } else if (t == 'zodiac') {
      _this.data.reqData['xgmember.zodiac'] = _this.data.objZodiacList[index].id;
      _this.setData({
        zodiac: _this.data.objZodiacList[index].name
      });
    } else if (t == 'constellation') {
      _this.data.reqData['xgmember.constellation'] = _this.data.objConstellationListC[index].id;
      _this.setData({
        constellation: _this.data.objConstellationListC[index].name
      });
    } else if (t == 'bloodtype') {
      _this.data.reqData['xgmember.blood'] = _this.data.objBloodTypeList[index].id;
      _this.setData({
        blood: _this.data.objBloodTypeList[index].name
      });
    } else if (t == 'child') {
      _this.data.reqData['xgmember.children'] = _this.data.objChildList[index].id;
      _this.setData({
        children: _this.data.objChildList[index].name
      });
    } else if (t == 'car') {
      _this.data.reqData['xgmember.car'] = _this.data.objCarList[index].id;
      _this.setData({
        car: _this.data.objCarList[index].name
      });
    } else if (t == 'house') {
      _this.data.reqData['xgmember.room'] = _this.data.objHouseList[index].id;
      _this.setData({
        room: _this.data.objHouseList[index].name
      });
    }
  },
  doCancel: function (e) {
    wx.navigateBack({})
  },
  doSave: function (e) {
    var req = this.data.reqData;
    for (let k in req) {
      if (req[k] == '' || req[k] == null || req[k] == undefined) {
        wx.showToast({
          title: '请选择完整信息',
          icon: 'none'
        })
        return;
      }
    }
    req.id=app.globalData.userid;
    app.doReq(app.globalData.SERVICE.USERCENTER.BASIC_INFO_XX_SAVE, req, 'get',
      function (res) {
        var data = res.data;
        if (data.code == 200 && data.type == 'SUCCESS') {
          wx.showToast({
            title: '保存成功',
            icon: 'success'
          });
          app.getUserInfoSyn({"openId":wx.getStorageSync('openid')}).then(function (v) {
            var pages = getCurrentPages();
            var currentPage = pages[pages.length - 1];//当前页面
            var pervPagge = pages[pages.length - 2];//上个页面
            pervPagge.setData({
              reloadFlag: true,
            })
            wx.navigateBack({});
          });
        }
      });
  },
})